package com.link.base.base.appduty.service;

import com.link.base.base.appduty.dao.mybatis.mapper.AppMenuSecInterMapper;
import com.link.base.base.appduty.model.AppMenuSecInter;
import com.link.base.base.appduty.model.AppQuickSetVO;
import com.link.base.base.monitor.MonitorObjectChange;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 13:49
 * @version 1.0
 */

@Service
public class AppMenuSecInterServiceImpl extends BasicServiceImpl<AppMenuSecInter> implements AppMenuSecInterService {

    @Resource
    private AppMenuSecInterMapper appMenuSecInterMapper;
    @Resource
    private MonitorObjectChange monitorObjectChange;

    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicMapper<AppMenuSecInter> getBasicMapper() {
        return appMenuSecInterMapper;
    }


    @Override
    public void beforUpsert(AppMenuSecInter appMenuSecInter) throws Exception {
        // 这里需要监控职责url变换,当变化时需要触发职责url变化
        monitorObjectChange.dutyChange(appMenuSecInter,"APP");
    }

    @Override
    public void beforDelete(AppMenuSecInter appMenuSecInter) throws Exception {
        // 这里需要监控职责url变换,当变化时需要触发职责url变化
        monitorObjectChange.dutyChange(appMenuSecInter,"APP");
    }

    @Override
    public void quickSetupSecMenu(AppQuickSetVO entity) throws Exception {
        List<AppMenuSecInter> addSecMenuList = entity.getAddSecMenuList();
        List<AppMenuSecInter> delSecMenuList = entity.getDelSecMenuList();
        if (null != addSecMenuList && addSecMenuList.size() > 0) {
            for (AppMenuSecInter appMenuSecInter : addSecMenuList) {
                if ("null".equals(String.valueOf(appMenuSecInter.getId()))) {
                    appMenuSecInter.setId(keyGenerateService.keyGenerate());
                }
                appMenuSecInterMapper.insert(appMenuSecInter);
            }
        }
        if (null != delSecMenuList && delSecMenuList.size() > 0) {
            for (AppMenuSecInter appMenuSecInter : delSecMenuList) {
                AppMenuSecInter item = appMenuSecInterMapper.queryByExamplePage(appMenuSecInter).get(0);
                if (null != item) {
                    appMenuSecInterMapper.deleteById(item);
                }
            }
        }
    }
}
